﻿Imports BUS
Imports DTO
Public Class NhapDiem

    Dim maMH As String
    Public Sub loadgrid()
        Dim maLH As Integer
        maLH = cmbLop.SelectedValue
        Dim hsBUS As New HocSinhBUS
        Dim DSHS As DataTable
        DSHS = hsBUS.DSHSNhapDiem(maLH)
        Dim dc As New DataColumn
        DSHS.Columns.Add(dc)
        gridNhapDiemHS.Columns(3).DataPropertyName = DSHS.Columns(2).ColumnName
        gridNhapDiemHS.DataSource = DSHS
        For i As Integer = 0 To gridNhapDiemHS.RowCount - 2
            gridNhapDiemHS.Rows(i).Cells(0).Value = i + 1
            gridNhapDiemHS.Rows(i).Cells(3).Value = 0
        Next
    End Sub
    Public Sub loadgridcapnhat()
        Dim nd As New NhapDiemBus
        Dim maLH As String = cmbLop.SelectedValue
        Dim maLKT As String = cmbLoaiKT.SelectedValue
        Dim hocky As String = cmbHocKi.SelectedValue
        maMH = nd.MaMonHoc(FormDangNhap.DangNhap1.tenuser)
        Dim DSHS As DataTable = nd.ChkCapNhat(maLH, maLKT, hocky, maMH)
        gridNhapDiemHS.Columns(3).DataPropertyName = DSHS.Columns(2).ColumnName
        gridNhapDiemHS.DataSource = DSHS
        For i As Integer = 0 To gridNhapDiemHS.RowCount - 2
            gridNhapDiemHS.Rows(i).Cells(0).Value = i + 1
        Next
    End Sub
    Private Sub NhapDiem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

    End Sub

    Private Sub NhapDiem_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim nd As New NhapDiemBus
        Dim dt As DataTable = nd.Lop(FormDangNhap.DangNhap1.tenuser)

        cmbLop.DataSource = dt

        cmbLop.DisplayMember = "TenLH"
        cmbLop.ValueMember = "MaLH"
        cmbLop.SelectedValue = "0"
        dt = nd.LoaiKiemTra
        cmbLoaiKT.DataSource = dt
        cmbLoaiKT.DisplayMember = "TenLKT"
        cmbLoaiKT.ValueMember = "MaLKT"
        dt = nd.HocKi
        cmbHocKi.DataSource = dt
        cmbHocKi.DisplayMember = "HocKy"
        cmbHocKi.ValueMember = "HocKy"

        cmbHocKi.Enabled = False
        cmbLoaiKT.Enabled = False
        chkSuaDiem.Enabled = False
        chkSuaDiem.Checked = False
        loadgrid()


    End Sub

    Private Sub cmbLop_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbLop.SelectedIndexChanged
        cmbLop.ValueMember = "MaLH"
        cmbHocKi.Enabled = True
        cmbLoaiKT.Enabled = True
        chkSuaDiem.Enabled = True
        loadgrid()

    End Sub

    Private Sub cmbHocKi_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbHocKi.SelectedIndexChanged
        cmbHocKi.ValueMember = "HocKy"
        chkSuaDiem.Checked = False
    End Sub

    Private Sub cmbLoaiKT_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbLoaiKT.SelectedIndexChanged
        cmbLoaiKT.ValueMember = "MaLKT"
        chkSuaDiem.Checked = False
    End Sub

    Private Sub chkSuaDiem_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSuaDiem.CheckedChanged
        If (chkSuaDiem.Checked) Then
            loadgridcapnhat()
        Else
            loadgrid()
        End If
    End Sub

    Private Sub butLuu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butLuu.Click
        Dim d As New DiemDTO
        Dim nd As New NhapDiemBus
        Dim madiem As Integer = nd.MaDiem
        maMH = nd.MaMonHoc(FormDangNhap.DangNhap1.tenuser)
        For i As Integer = 0 To gridNhapDiemHS.RowCount - 2
            If (gridNhapDiemHS.Rows(i).Cells(3).Value.ToString = String.Empty) Then
                MsgBox("Bạn chưa nhập điểm tại dòng có sô thứ tự : " + gridNhapDiemHS.Rows(i).Cells(0).Value.ToString, MsgBoxStyle.Exclamation, "Warning")
                Exit Sub
            End If
        Next
        d.MaMH = maMH
        d.MaLoaiKiemTra = cmbLoaiKT.SelectedValue
        d.HocKy = cmbHocKi.SelectedValue
        Dim dt As DataTable = nd.ChkCapNhat(cmbLop.SelectedValue, d.MaLoaiKiemTra, d.HocKy, d.MaMH)

        If (chkSuaDiem.Checked) Then
            For i As Integer = 0 To gridNhapDiemHS.RowCount - 2
                d.MaHS = gridNhapDiemHS.Rows(i).Cells(1).Value
                If (Double.TryParse(gridNhapDiemHS.Rows(i).Cells(3).Value, d.Diem) = False) Then
                    MsgBox("Dữ liệu không hợp lệ", MsgBoxStyle.Exclamation, "warning")
                    Exit Sub
                End If
                d.Diem = gridNhapDiemHS.Rows(i).Cells(3).Value
                If d.Diem < 0 Or d.Diem > 10 Then
                    MsgBox("Dữ liệu không hợp lệ", MsgBoxStyle.Exclamation, "warning")
                    Exit Sub
                End If
                nd.CapNhat(d)
                nd.CapNhatDiemTBMon(d.MaHS, d.HocKy, d.MaMH)
            Next
            MsgBox("Đã cập nhật lại điểm", MsgBoxStyle.OkOnly, "Success")
        End If

        If (chkSuaDiem.Checked = False) Then
            'Dim sql As String
            'sql = "delete from Diem where MaD in ( select dm.MaD from Diem dm,HocSinh hs where hs.MaLH=1 and dm.MaMH=1 and dm.HocKy=1 and dm.MaLKT=1)"
            'Dim dp As New DataProvider
            'dp.ExecuteNonQuery(sql)
            If (dt.Rows.Count > 0) Then
                MsgBox("Điểm đã có rồi", MsgBoxStyle.Information, "Information")
                Exit Sub
            End If
            For i As Integer = 0 To gridNhapDiemHS.RowCount - 2
                d.MaDiem = madiem
                d.MaHS = gridNhapDiemHS.Rows(i).Cells(1).Value

                If (Double.TryParse(gridNhapDiemHS.Rows(i).Cells(3).Value, d.Diem) = False) Then
                    MsgBox("Dữ liệu không hợp lệ", MsgBoxStyle.Exclamation, "warning")
                    Exit Sub
                End If
                d.Diem = gridNhapDiemHS.Rows(i).Cells(3).Value
                If d.Diem < 0 Or d.Diem > 10 Then
                    MsgBox("Dữ liệu không hợp lệ", MsgBoxStyle.Exclamation, "warning")
                    Exit Sub
                End If
                nd.NhapDiem(d)
                nd.CapNhatDiemTBMon(d.MaHS, d.HocKy, d.MaMH)
                madiem = madiem + 1
            Next
            MsgBox("Đã thêm mới điểm", MsgBoxStyle.OkOnly, "Success")
        End If
    End Sub

    Private Sub gridNhapDiemHS_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridNhapDiemHS.CellContentClick

    End Sub
End Class

